#*--------------------------------------------------------------------------------
# DESCRIPTION: Creating Appendix Figure G3-G4
#
#--------------------------------------------------------------------------------#

install.packages(c("fect","haven", "fixest", "ggplot2", "extrafont", "dplyr", "stringr", "flextable", "officer"))

library(dplyr)
library(extrafont)
library(fect)
library(fixest)
library(ggplot2)
library(haven)
library(stringr)
library(flextable)
library(officer)

font_import()
loadfonts(device = "win")

##############################################################################################################
##SET RELEVANT WORKING DIRECTORY
setwd("...") 

##############################################################################################################
##PREPARE DATA
data <- read_dta("data/clean/country_year_panel_bin_treatment.dta")
df.use <- data[!is.na(data$bin_treatment),]
df.use <- data.frame(df.use)
df.use <- get.cohort(data = df.use, D = "bin_treatment",index = c("panelid","bin"))

#############################################################################################################################

######################
##Create Figure G3##
######################

binothers.model.fect <- fect(trust_others_wo ~ bin_treatment, data = df.use, 
                             method = "fe", index = c("panelid","bin"), se = TRUE, nboots = 200, alpha = 0.05, 
                             parallel = TRUE, seed = 1234, force = "two-way", group = "Cohort")

ci_bounds <- binothers.model.fect[["est.group.output"]][["Cohort:3"]][["att.on.bound"]]
att <- binothers.model.fect[["group.output"]][["Cohort:3"]][["att.on"]]
time <- binothers.model.fect[["group.output"]][["Cohort:3"]][["time.on"]]
binothers.output <- data.frame(
  Time = time,
  ATT = att,
  CI.lower = ci_bounds[, 1],
  CI.upper = ci_bounds[, 2],
  Model = "FE"
)

dpi=600    #pixels per square inch
png("output/figures/appendix_g/figure_g3_a.png", width=6*dpi, height=5*dpi, res=dpi)
figure_g3_a <- ggplot(binothers.output, aes(x = Time, y = ATT)) +
  geom_hline(yintercept = 0, colour = "firebrick4") +
  geom_vline(xintercept = 0, colour = "firebrick4", linetype = 'dashed') +
  geom_errorbar(aes(x = Time, 
                    ymin = CI.lower,
                    ymax = CI.upper), width=.2, color = "black") + 
  geom_point(aes(x = Time, y = ATT), color = "black", size=1.85, shape=21, fill = "steelblue4") + 
  theme(text = element_text(family = "Palatino"),
        panel.background = element_rect(fill = 'white', color = 'black'),
        panel.grid.major = element_line(color = gray(1/4), size = 0.2, linetype = 'dotted'),
        panel.grid.minor = element_line(color = gray(1/4), size = 0.2, linetype = 'dotted'),
        axis.text.x = element_text(colour = "black"),
        axis.text.y = element_text(colour = "black"),
        axis.text=element_text(size=12), 
        axis.title=element_text(size=12),
        title=element_text(size=13)) +
  coord_cartesian(xlim=c(-3,3)) + ylim(-0.25,0.2) + scale_x_continuous(breaks=seq(-3, 3, 1)) + 
  ylab("Effect on Trust in Others") + 
  xlab("Time to 2008 Financial Crisis")
figure_g3_a
dev.off()

ci_bounds <- binothers.model.fect[["est.group.output"]][["Cohort:3"]][["att.off.bound"]]
att <- binothers.model.fect[["group.output"]][["Cohort:3"]][["att.off"]]
time <- binothers.model.fect[["group.output"]][["Cohort:3"]][["time.off"]]
binothers.output.exit <- data.frame(
  Time = time,
  ATT.OFF = att,
  CI.lower = ci_bounds[, 1],
  CI.upper = ci_bounds[, 2]
)

dpi=600    #pixels per square inch
png("output/figures/appendix_g/figure_g3_b.png", width=6*dpi, height=5*dpi, res=dpi)
figure_g3_b <- ggplot(binothers.output.exit, aes(x = Time, y = ATT.OFF)) +
  geom_hline(yintercept = 0, colour = "firebrick4") +
  geom_vline(xintercept = 0, colour = "firebrick4", linetype = 'dashed') +
  geom_errorbar(aes(x = Time, 
                    ymin = CI.lower,
                    ymax = CI.upper), width=.2, color = "black") + 
  geom_point(aes(x = Time, y = ATT.OFF), color = "black", size=1.85, shape=21, fill = "steelblue4") + 
  theme(text = element_text(family = "Palatino"),
        panel.background = element_rect(fill = 'white', color = 'black'),
        panel.grid.major = element_line(color = gray(1/4), size = 0.2, linetype = 'dotted'),
        panel.grid.minor = element_line(color = gray(1/4), size = 0.2, linetype = 'dotted'),
        axis.text.x = element_text(colour = "black"),
        axis.text.y = element_text(colour = "black"),
        axis.text=element_text(size=12), 
        axis.title=element_text(size=12),
        title=element_text(size=13)) +
  coord_cartesian(xlim=c(-2,3)) + ylim(-0.25,0.2) + scale_x_continuous(breaks=seq(-2, 3, 1)) + 
  ylab("Effect on Trust in Others") + 
  xlab("Time to Exiting 2008 Financial Crisis")
figure_g3_b
dev.off()


#############################################################################################################################

######################
##Create Figure G4##
######################

bingovt.model.fect <- fect(trust_govt_wo ~ bin_treatment, data = df.use, 
                           method = "fe", index = c("panelid","bin"), se = TRUE, nboots = 200, alpha = 0.05, 
                           parallel = TRUE, seed = 1234, force = "two-way", group = "Cohort")

ci_bounds <- bingovt.model.fect[["est.group.output"]][["Cohort:3"]][["att.on.bound"]]
att <- bingovt.model.fect[["group.output"]][["Cohort:3"]][["att.on"]]
time <- bingovt.model.fect[["group.output"]][["Cohort:3"]][["time.on"]]
bingovt.output <- data.frame(
  Time = time,
  ATT = att,
  CI.lower = ci_bounds[, 1],
  CI.upper = ci_bounds[, 2],
  Model = "FE"
)

dpi=600    #pixels per square inch
png("output/figures/appendix_g/figure_g4_a.png", width=6*dpi, height=5*dpi, res=dpi)
figure_g4_a <- ggplot(bingovt.output, aes(x = Time, y = ATT)) +
  geom_hline(yintercept = 0, colour = "firebrick4") +
  geom_vline(xintercept = 0, colour = "firebrick4", linetype = 'dashed') +
  geom_errorbar(aes(x = Time, 
                    ymin = CI.lower,
                    ymax = CI.upper), width=.2, color = "black") + 
  geom_point(aes(x = Time, y = ATT), color = "black", size=1.85, shape=21, fill = "steelblue4") + 
  theme(text = element_text(family = "Palatino"),
        panel.background = element_rect(fill = 'white', color = 'black'),
        panel.grid.major = element_line(color = gray(1/4), size = 0.2, linetype = 'dotted'),
        panel.grid.minor = element_line(color = gray(1/4), size = 0.2, linetype = 'dotted'),
        axis.text.x = element_text(colour = "black"),
        axis.text.y = element_text(colour = "black"),
        axis.text=element_text(size=12), 
        axis.title=element_text(size=12),
        title=element_text(size=13)) +
  coord_cartesian(xlim=c(-3,3)) + ylim(-0.25,0.2) + scale_x_continuous(breaks=seq(-3, 3, 1)) + 
  ylab("Effect on Trust in Government") + 
  xlab("Time to 2008 Financial Crisis")
figure_g4_a
dev.off()

ci_bounds <- bingovt.model.fect[["est.group.output"]][["Cohort:3"]][["att.off.bound"]]
att <- bingovt.model.fect[["group.output"]][["Cohort:3"]][["att.off"]]
time <- bingovt.model.fect[["group.output"]][["Cohort:3"]][["time.off"]]
bingovt.output.exit <- data.frame(
  Time = time,
  ATT.OFF = att,
  CI.lower = ci_bounds[, 1],
  CI.upper = ci_bounds[, 2]
)

dpi=600    #pixels per square inch
png("output/figures/appendix_g/figure_g4_b.png", width=6*dpi, height=5*dpi, res=dpi)
figure_g4_b <- ggplot(bingovt.output.exit, aes(x = Time, y = ATT.OFF)) +
  geom_hline(yintercept = 0, colour = "firebrick4") +
  geom_vline(xintercept = 0, colour = "firebrick4", linetype = 'dashed') +
  geom_errorbar(aes(x = Time, 
                    ymin = CI.lower,
                    ymax = CI.upper), width=.2, color = "black") + 
  geom_point(aes(x = Time, y = ATT.OFF), color = "black", size=1.85, shape=21, fill = "steelblue4") + 
  theme(text = element_text(family = "Palatino"),
        panel.background = element_rect(fill = 'white', color = 'black'),
        panel.grid.major = element_line(color = gray(1/4), size = 0.2, linetype = 'dotted'),
        panel.grid.minor = element_line(color = gray(1/4), size = 0.2, linetype = 'dotted'),
        axis.text.x = element_text(colour = "black"),
        axis.text.y = element_text(colour = "black"),
        axis.text=element_text(size=12), 
        axis.title=element_text(size=12),
        title=element_text(size=13)) +
  coord_cartesian(xlim=c(-2,3)) + ylim(-0.25,0.2) + scale_x_continuous(breaks=seq(-2, 3, 1)) + 
  ylab("Effect on Trust in Government") + 
  xlab("Time to Exiting 2008 Financial Crisis")
figure_g4_b
dev.off()
